package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zq.it.pojo.Major;
import com.zq.it.pojo.Student;
import com.zq.it.pojo.TClass;
import com.zq.it.pojo.exam.TClassTask;
import com.zq.it.service.admin.ClassService;
import com.zq.it.service.admin.StudentService;
import com.zq.it.service.common.BaseServiceImpl;
import com.zq.it.tool.Constant;
import com.zq.it.tool.StringUtil;

public class ClassServiceImpl extends BaseServiceImpl implements ClassService {

	@Override
	public List<TClass> getAllClass() {

		return getClassDao().findClassEtyList();
	}

	@Override
	public void removeClass(Integer id) {
		getClassDao().deleteClassEtyByID(id);

	}
	@Override
	public String creatClassHql(TClass t_class) {
		StringBuffer hql = new StringBuffer("from TClass as model where 1=1");
		String nameSql = null == t_class.getClassName()
				|| "".equals(t_class.getClassName())
						? ""
						: " and model.className like '%"
								+ t_class.getClassName() + "%'";
		String taskSql = t_class.getIshaveTask() < 0
				? ""
				: " and model.ishaveTask =" + t_class.getIshaveTask();
		hql.append(nameSql).append(taskSql);
		return hql.toString();
	}
	@Override
	public void renewalClass(TClass tclass) {
		if (tclass.getMajor().getMajorId() == 0)
			tclass.setMajor(null);
		getClassDao().updateClassEty(tclass);
	}

	@Override
	public List getClassById(Integer id) {
		return getClassDao().findClassEtyById(id);
	}

	@Override
	public List getMajorNameAndId() {
		return getMajorDao().findMajorNameAndId();
	}
	@SuppressWarnings("deprecation")
	@Override
	public List getClassListBySql(String hql) {

		return getClassDao().findClassEtyBySQL(hql);
	}

	@Override
	public List getClassByExmp(TClass tclass) {

		return getClassDao().findClassEtyByExmp(tclass);
	}

	@Override
	public void removeClass(TClass tclass) {
		getClassDao().deleteClassEtyByObj(tclass);
	}

	@Override
	public void removeAllClass(int[] ids) {
		getClassDao().deleteAllEty(ids);
	}

	@Override
	public List<TClass> importClassList(File classFile) {
		List<TClass> classList = getClassListFromStr(importxlsxFile(classFile));
		List<TClass> existClassList = new ArrayList<TClass>();

		for (int i = 0; i < classList.size(); i++) {
			TClass tclass = classList.get(i);
			if (getClassDao().findClassEtyBySQL("from TClass where classId="
					+ tclass.getClassId() + " and major.majorId="
					+ tclass.getMajor().getMajorId()).size() > 0)
				existClassList.add(tclass);
		}
		classList.removeAll(existClassList);
		if (!classList.isEmpty())
			getClassDao().saveClassEtyList(classList);
		return existClassList;
	}

	private List<TClass> getClassListFromStr(String str) {
		List<TClass> list = new ArrayList<TClass>();
		String[] firstArray = str.split(";");
		for (int i = 0; i < firstArray.length; i++) {
			String[] secondArray = firstArray[i].split(":");
			TClass tclass = new TClass();
			tclass.setClassId(Integer.parseInt(secondArray[0]));
			tclass.setClassName(secondArray[1]);
			com.zq.it.pojo.Major m = new Major();
			m.setMajorId(Integer.parseInt(secondArray[2]));
			tclass.setMajor(m);
			list.add(tclass);
		}
		return list;
	}

	@Override
	public ByteArrayInputStream exportClassList(String[] headTitle,
			List<TClass> list) {
		return new ByteArrayInputStream(exportToExcel(list, headTitle,
				com.zq.it.tool.HeadTitle.EN_CLASSHEAD.getArray(),
				"com.zq.it.pojo.TClass").toByteArray());
	}

	@Override
	public List getTClass(String hql) {
		return getClassDao().findClassEtyBySQL(hql);
	}

	@Override
	public void addClass(TClass tClass) {
		getClassDao().save(tClass);
	}

	@Override
	public void updateClass(TClass tclass) {
		tclass.setClassName(StringUtil.trim(tclass.getClassName()));
		getClassDao().update(tclass);
	}

	@Override
	public void deleteClass(int[] ids) {
		long i = 0;
		List temp;
		TClassTask classTask = null;
		for (int id : ids) {
			TClass tclass = getClassDao().findById(id);
			List<Student> studentList = getStudentDao().findByClassId(id);
			for (Student student : studentList) {
				long stuId[] = {student.getId()};
				((StudentService) WebApplicationContextUtils
						.getWebApplicationContext(ServletActionContext
								.getRequest().getSession().getServletContext())
						.getBean("studentService")).delete(stuId);
			}
			temp = classTaskDAO.findByHql(
					"from TClassTask t where t.TClass.classId=" + id);
			if (temp != null) {
				for (int j = 0; j < temp.size(); j++) {
					classTask = (TClassTask) temp.get(j);
					deleteDir(new File(Constant.CLASS_TASK + classTask.getDir()));
					classTaskDAO.delete(classTask);
				}
			}
			classPaperDAO.deleteByHql(
					"delete from TClassPaper t where t.TClass.classId=" + id);
			getClassDao().delete(tclass);
		}
	}

	@Override
	public List getAllTeacher() {
		return getTeacherDao().findAll();
	}

	@Override
	public TClass getClass(String className) {
		List data = getClassDao().findByClassName(className);
		if (null != data && !data.isEmpty()) {
			return (TClass) data.get(0);
		} else {
			return null;
		}
	}
}
